#include "CocosBase.h"

using namespace cocos2d;


CCharacter::CCharacter()
: m_ptPos(0, 0)
, m_direction(0)
, m_pParent(NULL)
, m_bEnable(false)
, m_pSprBack(NULL)
{

}


//---------------------------------------------------------------------------------------------------------------------------/
CCharacter::~CCharacter()
{

}


//---------------------------------------------------------------------------------------------------------------------------/
float CCharacter::GetWidth()
{
	if(NULL == m_pSprBack)
	{
		return 0;
	}

	return m_pSprBack->getTextureRect().size.width;
}


//---------------------------------------------------------------------------------------------------------------------------/
float CCharacter::GetHeight()
{
	if(NULL == m_pSprBack)
	{
		return 0;
	}

	return m_pSprBack->getTextureRect().size.height;
}


//---------------------------------------------------------------------------------------------------------------------------/
void CCharacter::SetPosition(float xPos, float yPos)
{
	m_ptPos.x = xPos;
	m_ptPos.y = yPos;
}


//---------------------------------------------------------------------------------------------------------------------------/
void CCharacter::SetPosition(const CCPoint &pt)
{
	m_ptPos = pt;
}


//---------------------------------------------------------------------------------------------------------------------------/
void CCharacter::SetDirection(float angle)
{
	m_direction = angle;
}


//---------------------------------------------------------------------------------------------------------------------------/
void CCharacter::SetDirectionRad(float rad)
{
	m_direction = rad * 180.0f / 3.14159f;
}

bool CCharacter::InitWithNameInFile(CCNode *pParent, const char *szName, int zOrder, CCPoint ptPos, float rotation)
{
	assert(NULL != pParent);
	m_pParent   = pParent;

	m_pSprBack = CCSprite::spriteWithFile(szName);

	SetPosition(ptPos);
	SetDirection(rotation);
	UpdatePosition();

	m_zOrder = zOrder;
	pParent->addChild(m_pSprBack, zOrder);

	SetRadius(m_pSprBack->getTextureRect().size.width * 0.5f);

	InitState();

	return true;
}
//---------------------------------------------------------------------------------------------------------------------------/
bool CCharacter::InitWithNameInCache(CCNode *pParent, const char *szName, int zOrder, CCPoint ptPos, float rotation)
{
	assert(NULL != pParent);
	m_pParent   = pParent;

	m_pSprBack = CCSprite::spriteWithSpriteFrameName(szName);

	SetPosition(ptPos);
	SetDirection(rotation);
	UpdatePosition();

	m_zOrder = zOrder;
	pParent->addChild(m_pSprBack, zOrder);

	SetRadius(m_pSprBack->getTextureRect().size.width * 0.5f);

	InitState();

	return true;
}


//---------------------------------------------------------------------------------------------------------------------------/
bool CCharacter::ChangeSprite(const char *szName)
{
	assert(NULL != m_pParent);

	m_pParent->removeChild(m_pSprBack,true);
	m_pSprBack = CCSprite::spriteWithSpriteFrameName(szName);
	m_pParent->addChild(m_pSprBack, m_zOrder);

	SetRadius(m_pSprBack->getTextureRect().size.width * 0.5f);
	UpdatePosition();

	return true;
}


//---------------------------------------------------------------------------------------------------------------------------/
void CCharacter::UpdatePosition()
{
	if(NULL != m_pSprBack)
	{
		m_pSprBack->setPosition(m_ptPos);
		m_pSprBack->setRotation(m_direction);
	}

}


//---------------------------------------------------------------------------------------------------------------------------/
void CCharacter::Move(float xAdd, float yAdd)
{
	m_ptPos.x += xAdd;
	m_ptPos.y += yAdd;
}

void CCharacter::Move(CCPoint pt)
{
	m_ptPos.x += pt.x;
	m_ptPos.y += pt.y;

	m_pSprBack->setPosition(m_ptPos);
}

//---------------------------------------------------------------------------------------------------------------------------/
float CCharacter::CalDistance(const CCharacter &anotherChar)
{
	return 0;
}
